<?php
namespace App\Http\Controllers\ApiAdmin;

use App\DBs\Stock\LayerDB;
use Illuminate\Http\Request;

class StoLayerController extends BaseController
{
    /**
     * 库素材
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new LayerDB();
    }

    /**
     * 视频列表
     */
    public function getIndex(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $page = $request->post('page',0);
        $size = $request->post('size',$this->limit);
        $pageStart = $page * $size;
        $vid = $request->post('vid',0);
        //梳理数据
        $resDb = $this->db->getAdminIndex($pageStart,$size,$vid);
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-3);
        }
        $dataArr = $resDb['data'];
        $total = $resDb['total'];
        return $this->toAdminJson(0, $dataArr, $total);
    }

    /**
     * 新增表单
     */
    public function setAdd(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $vid = $request->post('vid',0);
        $type = $request->post('type',0); //1文件地址，2文字，3特效地址
        $link = $request->post('link',''); //地址或文字
        $style = $request->post('style',''); //样式
        $types = $this->db->getTypes();
        if (!array_key_exists($type, $types) || !$fileLink) {
            return $this->toAdminJson(-2);
        }
        // 获取该库最大序号的记录
        $resDb2 = $this->db->getAdminMaxOne($vid);
        if ($resDb2['code']!=0) {
            return $this->toAdminJson(-3);
        }
        $serialMax = $resDb2['data']['serial'];
        //梳理数据
        $dataArr = array(
            'type' => $type,
            'serial' => $serialMax + 1,
            'link' => $ink,
            'created_at' => time(),
        );
        $resDb = $this->db->setAdd($dataArr);
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-500);
        }
        return $this->toAdminJson(0);
    }

    /**
     * 更新表单
     */
    public function setUpdate(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $layerId = $request->post('layerId',0);
        $type = $request->post('type',0);
        $link = $request->post('link','');
        $style = $request->post('style',''); //样式
        $types = $this->db->getTypes();
        if (!$layerId || !array_key_exists($type, $types) || !$link) {
            return $this->toAdminJson(-2);
        }
        //看看有没有
        $resDb3 = $this->db->getAdminOne($layerId);
        if ($resDb3['code']!=0) {
            return $this->toAdminJson(-3);
        }
        //梳理数据
        $dataArr = array(
            'type' => $type,
            'link' => $link,
            'updated_at' => time(),
        );
        $resDb = $this->db->setAdminUpdate($layerId,$dataArr);
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-500);
        }
        return $this->toAdminJson(0);
    }

//    /**
//     * 删除一个
//     */
//    public function setDel(Request $request)
//    {
//        //接口权限值
//        if ($request->adminAccess != $this->adminAccess) {
//            return $this->toAppJson(-1);
//        }
//        //获取参数
//        $vid = $request->post('vid',0);
//        //梳理数据
//        $resDb = $this->db->setAdminDel($vid, 1);
//        if ($resDb['code']!=0) {
//            return $this->toAdminJson(-500);
//        }
//        return $this->toAdminJson(0);
//    }
}